import {getAge} from '../../third-utils/time/index.js';
Page({
	data: {
		openId: '',
		nickname: '',
		curId: '',
		categroyList:[],
		reportDetail: {},
		content: {},
    birthday:'',
    isShowAll:false,
		infoList:[],
		babyInfo: {}
	},
	onLoad(e) {
		this.setData({
			curId: e.id
		})
		
		
		wx.request({
			url: getApp().globalData.apiBaseUrl + 'recordType/findList',
			method: 'post',
			data:{
				level:1
			},
			success: (res) => {
				console.log(res, 122)
				if (res.data.code == 200) {
					this.setData({
						categroyList: res.data.data,
					})
				}
			},
			failed: (err) => {
				console.log('err:', err);
			}
		})
		
		
		wx.request({
			url: getApp().globalData.apiBaseUrl + 'babyBMI/findPage',
			method: 'post',
			data:{
				babyId:32,
				pageNo:1,
				pageSize:1,
			},
			success: (res) => {
				console.log(res, 122)
				if (res.data.code == 200) {
					this.setData({
						infoList: res.data.data.list,
					})
				}
			},
			failed: (err) => {
				console.log('err:', err);
			}
		})
	},
	onShow() {
		this.getList()

		// console.log(getApp().globalData, 22222)
		// console.log(getApp(), 22222)
		// const {
		// 	app
		// } = getApp().getYouZanYunSdk();
		// app
		// 	.getUserInfo()
		// 	.then(({
		// 		userInfo,
		// 		state
		// 	}) => {
		// 		console.log(userInfo, state, 1111)

		// 		this.setData({
		// 			openId: userInfo.openId,
		// 			nickname: userInfo.nickname
		// 		})
		// 		this.getList()
		// 	})
		// 	.catch((error) => {});
	},
	openBaby(){
    this.setData({
      isShowAll:true
    })
  },
	getInfo() {

		request({
			path: 'api/baby/view',
			method: 'get',
			data: {
				id: this.data.curId
			},
			succeed: (res) => {

				console.log(res, 122)
				if (res.code == 200) {
					this.setData({
						babyInfo: res.data,
					})
				}
			},
			failed: (err) => {
				console.log('err:', err);
			}
		})
		this.setData({
			reportDetail: {},
			content: {}
		})
		request({
			path: 'api/allergyReport/findPage',
			method: 'POST',
			data: {
				pageSize: 2,
				babyId: this.data.curId,
			},
			succeed: (res) => {

				console.log(res, 122)
				if (res.code == 200) {
					if (res.data.list.length > 0) {

						this.setData({
							reportDetail: res.data.list[0],
							content: JSON.parse(res.data.list[0].content)
						})
					}
				}
			},
			failed: (err) => {
				console.log('err:', err);
			}
		})
	},
	toBodyInfo(){
	    wx.navigateTo({
	      url: '/pages/event-report/index?babyId='+this.data.curId
	    })
	  },
	  toEventList(){
      console.log(111)
	    wx.navigateTo({
	      url: '/pages/event-report/index?babyId='+this.data.curId
	    })
	  },
	  toEventAdd(e){
      console.log(222)
	    let idx=e.currentTarget.dataset.index
	    let route=[
	      {
	        title:'消化道',
	        path:'/pages/event-xhd/index?cateId='
	      }, {
	        title:'皮肤',
	        path:'/pages/event-pf/index?cateId='
	      }, {
	        title:'呼吸道',
	        path:'/pages/event-hxd/index?cateId='
	      }, {
	        title:'医疗',
	        path:'/pages/event-yl/index?cateId='
	      },{
	        title:'喂奶',
	        path:'/pages/event-wn/index?cateId='
	      },{
	        title:'过敏原',
	        path:'/pages/event-gmy/index?cateId='
	      },{
	        title:'辅食',
	        path:'/pages/event-fs/index?cateId='
	      },{
	        title:'用药',
	        path:'/pages/event-medication/index?cateId='
	      },
	    ]
	    route.some(item=>{
	      if(item.title==this.data.categroyList[idx].name){
	
	        wx.navigateTo({
	          url: item.path+this.data.categroyList[idx].id+'&babyId='+this.data.curId
	        })
	      }
	    })
	  },
	getList() {
		wx.request({
			url: getApp().globalData.apiBaseUrl + 'baby/findPage',
			method: 'POST',
			data: {
				pageSize: 20,
				userId: 'ojoaU5K-koC_63sip1TWNgjJi7pY'
			},
			success: (res) => {

				console.log(res, 122)
				let info = res.data
				if (info.code == 200) {

					this.setData({
						babyList: info.data.list
					})

					if (this.data.curId) {
						this.getInfo()
					} else {
						if (this.data.babyList.length <= 0) {
							wx.navigateTo({
								url: '/pages/add/index',
							})
						} else {
							this.setData({
								curId: this.data.babyList[0].id
							})
							this.getInfo()
						}

					}
				}
			},
			failed: (err) => {
				console.log('err:', err);
			}
		})
	},
	getInfo() {

		wx.request({
			url: getApp().globalData.apiBaseUrl + 'baby/view',
			method: 'get',
			data: {
				id: this.data.curId
			},
			success: (res) => {

				console.log(res, 122)
				if (res.data.code == 200) {
					this.setData({
						babyInfo: res.data.data,
						birthday: getAge(res.data.data.birthday)
					})
				}
			}, 
			failed: (err) => {
				console.log('err:', err);
			}
		})
		this.setData({
			reportDetail: {},
			content: {}
		})
		wx.request({
			url: getApp().globalData.apiBaseUrl + 'allergyReport/findPage',
			method: 'POST',
			data: {
				pageSize: 2,
				babyId: this.data.curId,
			},
			success: (res) => {

				console.log(res, 122)
				if (res.data.code == 200) {
					if (res.data.data.list.length > 0) {

						this.setData({
							reportDetail: res.data.data.list[0],
							content: JSON.parse(res.data.data.list[0].content)
						})
					}
				}
			},
			failed: (err) => {
				console.log('err:', err);
			}
		})
	},
	selectBaby(e) {
		this.setData({
      curId: e.currentTarget.dataset.id,
      isShowAll:false,
		})
		this.getInfo()
	},
	addBaby() {
		wx.navigateTo({
			url: '/pages/add/index',
		})
	},
	toCe() {

		wx.navigateTo({
			url: '/pages/index1/index',
		})
	},
	toBao(e) {
    wx.navigateTo({
      url: '/pages/report-detail/index?id=1',
    })
	},
	toKefu() {
		wx.navigateTo({
			url: '/pages/event-report/index',
		})
		// const sdk = getApp().getYouZanYunSdk();
		// sdk
		// 	.openWebview("https://shop121239703.youzan.com/v3/im/index?kdt_id=112185116", "navigate", {
		// 		youzan: true
		// 	})
		// 	.then(() => success())
		// 	.catch(() => fail());
	},
	toAdd() {
		wx.navigateTo({
			url: '/pages/add/index?id=' + this.data.curId,
		})
	},
	toPage: function(e) {
		wx.navigateTo({
			url: '/pages/index/index',
		})
	},
});
